﻿using MediatR; 
using S.Organization.Domian.dto;
using S.Organization.Domian.Model;
using S.Organization.Infrastructure;
using S.Organization.Read.API.Application.Command.ClientCommand;

namespace S.Organization.Read.API.Application.CommandHandler.ClientCommandHandler
{
    public class ClientCommandHandler : IRequestHandler<ClientCommand, List<ClinetDTO>>
    {
        private readonly IBaseRepository<Client> _repository;


        public ClientCommandHandler(IBaseRepository<Client> repository)
        {
            _repository = repository;
        }

        public async Task<List<ClinetDTO>> Handle(ClientCommand request, CancellationToken cancellationToken)
        {
            var query = await _repository.AllAsync();

            List<ClinetDTO> list = new List<ClinetDTO>();

            foreach (var item in query)
            {
                ClinetDTO clinetDTO = new ClinetDTO();
                clinetDTO.ID = item.ClientId;
                clinetDTO.Name=item.ClientName;
                clinetDTO.Address=item.pcaTextArr;
                list.Add(clinetDTO);
            }
            return list;
        }
    }
}
